/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_web
 * FILE       : SiteMapTag.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 4, 2012  MyCodeTalks			       Created
 */

package net.lwf.web.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.entity.WebUser;
import net.lwf.jaxb.service.TabMenuServiceFactory;
import net.lwf.jaxb.stubs.tabmenu.SubjectInfo;
import net.lwf.jaxb.stubs.tabmenu.TopicInfo;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IWebConstant;

public class SiteMapTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;


	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		this.setInfo(request);
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}
	
	private void setInfo(HttpServletRequest request) {
		String grade = this.getGrade(request);
		if(grade != null) {
			request.setAttribute(IWebConstant.GRADE, grade);
			String subject = request.getParameter(IWebConstant.SUBJECT);
			String topic = request.getParameter(IWebConstant.TOPIC);
			//Remove Space delemeter (|SPACE| if present)
			//topic = StringUtils.replace(topic, ICommonConstant.REQUEST_SPACE_DELEMETER, ICommonConstant.EMPTY_SPACE);
			
			if(subject != null && topic != null) {
				TopicInfo topicinfo = TabMenuServiceFactory.getInstance().getMenu(grade, subject, topic);
				request.setAttribute(IWebConstant.TOPIC_MENU_INFO, topicinfo);
			}
			else if(subject != null && topic == null) {
				SubjectInfo sujectinfo = TabMenuServiceFactory.getInstance().getMenu(grade, subject);
				request.setAttribute(IWebConstant.SUBJECT_MENU_INFO, sujectinfo);
			}
			else {
				//Do nothing
			}
		}
	}
	
	private String getGrade(HttpServletRequest request) {
		WebUser user = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		if(user != null) {
			return user.getUser().getGrade();
		}
		return null;
	}

}
